<?php
    include_once './config.php';
    $user = checkLogin();
    include_once './config.php';
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $username = $_POST['username'];
        $phone = $_POST['phone'];
        $can_order = $_POST['can_order'] == 'on' ? 1 : 0;
        $password = $_POST['password'];
        if($password){
            $password = md5($password);
        }
        $commission = $_POST['commission'];
        $user_id = $_POST['user_id'];
        $msg = '';
        $error = false;
        if(!$error){
            if($user_id){
                //编辑
                $query = "select * from whw_user where id=".getInt($user_id);
                $user = $objDBCD14->queryUniqueObject($query);
                if (!$user) {
                    $msg = '账号不存在!';
                    $error = true;
                } else {
                    $query = "select * from whw_user where phone=".quote($phone)." and id <> ".getInt($user_id);
                    $user = $objDBCD14->queryUniqueObject($query);
                    if ($user) {
                        $msg = '登录手机已存在!';
                        $error = true;
                    } else {
                        $query = "update whw_user set username=".quote($username).",phone=".quote($phone).",commission=".getFloat($commission).",can_order=".getInt($can_order).",updatetime=".time();
                        if($password){
                            $query .= ",password=".quote($password);
                        }
                        $query .= " where id=".getInt($user_id);
                        $objDBCD14->execute($query);
                        $msg = "编辑成功";
                    }
                }
            } else {
                // 添加
                $query = "select * from whw_user where phone=".quote($phone);
                $user = $objDBCD14->queryUniqueObject($query);
                if ($user) {
                    $msg = '登录手机已存在!';
                    $error = true;
                } else {
                    $query = "insert into whw_user (username,phone,password,commission,type,can_order,createtime,updatetime) values (".quote($username).",".quote($phone).",".quote($password).",".getFloat($commission).",2,".getInt($can_order).",".time().",".time().")";
                    $objDBCD14->execute($query);
                    $msg = "添加成功";
                }
            }
            
        }
        echo json_encode(['code'=>$error ? 0 : 1,'msg'=>$msg]);
        exit;
    }
    $user = [];
    $user_id = '';
    if(isset($_GET['user_id']) && $_GET['user_id']){
        $user_id = $_GET['user_id'];
        $query = "select * from whw_user where id=".getInt($user_id);
        $user = $objDBCD14->queryUniqueObject($query);
    }
?>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>添加师傅</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style>
        .container{
            margin: 0;
            padding: 20px 10px;
        }
    </style>
</head>
<body ontouchstart>
    <div class="container" id="container">
        <form class="layui-form layui-form-pane" action="" id="myForm">
          <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="username" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input"
                value="<?= $user->username ?>">    
            </div>
          </div>
           <div class="layui-form-item">
            <label class="layui-form-label">登录手机</label>
            <div class="layui-input-block">
                <input type="text" name="phone" required lay-verify="required" placeholder="请输入登录手机" autocomplete="off" class="layui-input"
                value="<?= $user->phone ?>">    
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">分佣比例(%)</label>
            <div class="layui-input-block">
                <input type="number" name="commission" required lay-verify="required" placeholder="请输入分佣比例" autocomplete="off" class="layui-input"
                value="<?= $user->commission ?>">    
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">登录密码</label>
            <div class="layui-input-block">
                <?php if($user_id) {  ?>
                    <input type="password" name="password"  placeholder="为空不修改" autocomplete="off" class="layui-input">    
                <?php  } else { ?>
                    <input type="password" name="password"  required lay-verify="required" placeholder="请输入登录密码" autocomplete="off" class="layui-input">   
                <?php  }  ?> 
                
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">授权订单</label>
            <div class="layui-input-block">
                <input type="checkbox" <?= $user->can_order ? 'checked' : '' ?> name="can_order" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF"
                class="layui-input">
            </div>
          </div>
          <input type="hidden" name="user_id" value="<?=$user_id?>"/>
          <button type="submit" class="layui-btn layui-btn-danger layui-btn-lg"
          style="width: 100%;" id="submitForm">提交</button>
        </form>
    </div>
    <!-- 引入 layui.js -->
    <script src="./layui/layui.js"></script>
    <script>
        layui.use(['jquery','layer', 'form','code'], function(){
            var $ = layui.$,layer = layui.layer,form = layui.form,code = layui.code;
            $("#myForm").submit(function(){
                var formValues = $("#myForm").serializeArray();
                var param = {}
                for(var i = 0;i < formValues.length;i++){
                    param[formValues[i].name] = formValues[i].value
                }
                $.post("worker-add.php",param,function(res){
                    res = JSON.parse(res)
                    layer.msg(res.msg);
                    if(res.code == 1){
                        var index = parent.layer.getFrameIndex(window.name); 
                        setTimeout(function(){
                            parent.layer.close(index);
                             parent.location.reload()
                        },1000)
                    }
                })
                return false
            })
        });
    </script>  
</body>
</html>